package com.coscon.model.api;

import lombok.Data;

import java.io.Serializable;

/**
 * 可以作为通用的人api接口调用返回对象
 * Created by 409390047@qq.com on 5/30/2019
 */

@Data
public class ApiResponse<T> implements Serializable {

    /**
     * 返回值 0成功 1失败
     */
    private int code;
    private String msg = "SUCCESS";
    /**
     * 可以放入任意对象的返回数据
     */
    private T data;

    public ApiResponse(){}

    public ApiResponse(int code) {
        this.code = code;
    }

    public ApiResponse(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public ApiResponse(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public ApiResponse(int code, T data) {
        this.code = code;
        this.data = data;
    }


    public static <T> ApiResponse<T> createBySuccess() {
        return new ApiResponse<T>(ResponseCode.SUCCESS.getCode());
    }

    public static <T> ApiResponse<T> createBySuccess(T data) {
        return new ApiResponse<T>(ResponseCode.SUCCESS.getCode(), data);
    }

    public static <T> ApiResponse<T> createBySuccess(String msg, T data) {
        return new ApiResponse<T>(ResponseCode.SUCCESS.getCode(), msg, data);
    }

    public static <T> ApiResponse<T> createByError() {
        return new ApiResponse<T>(ResponseCode.ERROR.getCode());
    }

    public static <T> ApiResponse<T> createByError(String msg) {
        return new ApiResponse<T>(ResponseCode.ERROR.getCode(), msg);
    }
    public static <T> ApiResponse<T> createByError(ResponseCode responseCode) {
        return new ApiResponse<T>(responseCode.getCode(), responseCode.getMsg());
    }

}
